package com.boot.security.server.controller;

import com.boot.security.server.ccccc.DocRepository;
import com.boot.security.server.ccccc.DocService;
import com.boot.security.server.ccccc.ESDocument;
import com.boot.security.server.utils.UUIDUtils;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author LF
 * @date 2020/8/17 - 14:26
 */
@RestController
@RequestMapping("/doc")
public class DocTest {
    @Autowired
    private DocService docService;

    @Autowired
    private DocRepository docRepository;


    @PostMapping("/add")
    @ApiOperation(value = "根据id获取")
    public ESDocument save(/*@RequestBody ESDocument esDocument*/){
        ESDocument esDocument=new ESDocument ();
        esDocument.setId(1);
        esDocument.setTitle("我是中国人");
        esDocument.setAuthor("LF");
        esDocument.setContent("我是中国人，我爱祖国");
        esDocument.setCreateTime(new Date());
        esDocument.setUpdateTime(new Date());
        docService.save(esDocument);

        esDocument.setId(2);
        esDocument.setTitle("你是美国人");
        esDocument.setAuthor("LF");
        esDocument.setContent("你是中国人，打倒川普");
        esDocument.setCreateTime(new Date());
        esDocument.setUpdateTime(new Date());
        docService.save(esDocument);
        return esDocument;
    }

    @RequestMapping("/sel")
    public List<ESDocument> getByName(String name, Integer id) {
        List<ESDocument> esDocuments = docService.getByName(name, id);
        return esDocuments;
    }

   /* @GetMapping("/seach")
    public Iterable<ESDocument> search() {
        Iterable<ESDocument> search = docService.search();
        return search;
    }*/

    @PostMapping("/search")
    public Object search(String title, String content, Integer id){
        HashMap<String,Object> map=new HashMap<>();
        StopWatch stopWatch=new StopWatch();
        stopWatch.start();

        BoolQueryBuilder builder= QueryBuilders.boolQuery();
        //matchQuery先将搜索词进行分词然后再进行查询只要里面包含任意一个词都可以被查出
//            builder.should(QueryBuilders.matchPhraseQuery("title",blogvo.getKeyword()));
//            builder.should(QueryBuilders.matchPhraseQuery("content",blogvo.getKeyword()));

        //matchPhraseQuery搜索词不进行分词，精准匹配搜索词，如何不一致则无法进行匹配
        //相当于or
        builder.should(QueryBuilders.matchPhraseQuery("title",title));
        builder.should(QueryBuilders.matchPhraseQuery("content",content));
        Page<ESDocument> search= (Page<ESDocument>) docRepository.search(builder);
        List<ESDocument> list = search.getContent();
        map.put("list",list);
        stopWatch.stop();
        long totalTimeMillis = stopWatch.getTotalTimeMillis();
        map.put("duration",totalTimeMillis);
        return map;
    }
}
